package com.mvvm.start.base.utils

import android.app.Service
import android.content.Context
import android.os.Build
import android.provider.Settings
import android.text.TextUtils
import android.view.WindowManager
import android.util.DisplayMetrics
import java.lang.Exception

object NavigationBar {
    fun isNavigationBarHasShown(context: Context): Boolean {
        val brand = Build.BRAND
        var deviceInfo: String? = null
        if (brand.equals("HUAWEI", ignoreCase = true)) {
            deviceInfo = "navigationbar_is_min"
        } else if (brand.equals("XIAOMI", ignoreCase = true)) {
            deviceInfo = "force_fsg_nav_bar"
        }
        if (!TextUtils.isEmpty(deviceInfo)) {
            val navigationBarIsMin = Settings.Global.getInt(context.contentResolver, deviceInfo, 0)
            return navigationBarIsMin != 1
        }
        val windowService = context.getSystemService(Service.WINDOW_SERVICE)
        return if (windowService == null) {
            false
        } else {
            val windowManager = windowService as WindowManager
            val display = windowManager.defaultDisplay
            val realDisplayMetrics = DisplayMetrics()
            display.getRealMetrics(realDisplayMetrics)
            val realHeight = realDisplayMetrics.heightPixels
            val realWidth = realDisplayMetrics.widthPixels
            val displayMetrics = DisplayMetrics()
            display.getMetrics(displayMetrics)
            val displayHeight = displayMetrics.heightPixels
            val displayWidth = displayMetrics.widthPixels
            if (displayHeight > displayWidth) {
                if (displayHeight + getNavigateHeight(context) > realHeight) {
                    return false
                }
            } else if (displayWidth + getNavigateHeight(context) > realWidth) {
                return false
            }
            realWidth - displayWidth > 0 || realHeight - displayHeight > 0
        }
    }

    fun getNavigateHeight(context: Context): Int {
        try {
            val resources = context.resources
            val resId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
            if (resId > 0) return resources.getDimensionPixelSize(resId)
        } catch (e: Exception) {
        }
        return 0
    }

    fun checkDeviceHasNavigationBar(context: Context): Boolean {
        var hasNavigationBar = false
        val rs = context.resources
        val id = rs.getIdentifier("config_showNavigationBar", "bool", "android")
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id)
        }
        try {
            val systemPropertiesClass = Class.forName("android.os.SystemProperties")
            val m = systemPropertiesClass.getMethod("get", String::class.java)
            val navBarOverride = m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String
            if ("1" == navBarOverride) {
                hasNavigationBar = false
            } else if ("0" == navBarOverride) {
                hasNavigationBar = true
            }
        } catch (e: Exception) {
        }
        return hasNavigationBar
    }
}